package com.dashan.servlets;

import static com.dashan.Constants.*;
import static com.dashan.servlets.Parameter.Option.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;

import com.dashan.backend.XmlSerializable;
import com.dashan.backend.datastore.DsPosition;
import com.dashan.backend.datastore.PositionHistoryDao;
import com.dashan.backend.datastore.UserDao;
import com.dashan.backend.util.XmlHelper;

@SuppressWarnings("serial")
public class GetPositionHistoryServlet extends DashanPrivateServlet {

    @Override
    public ArrayList<Parameter> getParameterList() {
        ArrayList<Parameter> res = super.getParameterList();
        res.add(new Parameter(COUNT, Optional, Integer.class, "Last n records"));
        res.add(new Parameter(DATE_FROM, Optional, Date.class, "Milliseconds since January 1/1/1970 GMT"));
        res.add(new Parameter(DATE_TO, Optional, Date.class, "Milliseconds since January 1/1/1970 GMT"));
        return res;
    }

    @Override
    public String getUrlPattern() {
        return "/getpositionhistory";
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        TreeMap<String, Parameter> params = loadParamters(req);
        UserDao dao = new UserDao();
        PositionHistoryDao positionDao = new PositionHistoryDao();
        try {
            String userName = params.get(USERNAME).stringValue(); 
            if (!dao.isUserExist(userName)) {
                throw new ServletException(String.format("User \"%s\" is not exist!", userName));
            }
            
            List<DsPosition> result;
            if (params.get(COUNT).hasValue()) {
                result = positionDao.getPositionHistory(userName, params.get(COUNT).integerValue());
            } else if (params.get(DATE_FROM).hasValue() 
                       && (params.get(DATE_TO).hasValue())) {
                System.out.print((params.get(DATE_FROM).dateValue().toString()));
                System.out.print((params.get(DATE_TO).dateValue().toString()));
                result = positionDao.getPositionHistory(userName, 
                        params.get(DATE_FROM).dateValue(), 
                                (params.get(DATE_TO).dateValue()));
            } else {
                result = positionDao.getPositionHistory(userName);
            }
            
            Document xmlDoc = XmlHelper.createXmlDocument("response");
            for (XmlSerializable position : result) {
                xmlDoc.getDocumentElement().appendChild(position.getXmlElement(xmlDoc));
            }
            resp.getWriter().print(XmlHelper.xml2String(xmlDoc));
        } finally {
            dao.commit();
            positionDao.commit();
        }
    }
}
